package com.otr.ai.service;

import cn.hutool.extra.spring.SpringUtil;
import com.otr.ai.domain.Order;
import com.otr.ai.domain.Product;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class ProductTools {

  private static final List<Product> dataList = mockDBData();

  private static final RestTemplate restTemplate = SpringUtil.getBean("restTemplate");

  @Tool(description = "根据id查询商品")
  public Function<Product, List<Product>> getProductByCondition() {
    return req -> {
      log.info("Tool 'getItemById' called with request: {}", req);
      List<Product> response = this.queryListByCondition(req);

      log.info("========Tool 'getItemById' response: {}", response);

      return response;
    };
  }

  @Tool(description = "删除商品")
  public Boolean deleteById(@ToolParam(description = "商品id") Long id) {
    log.info("Tool 'deleteById' called with request: {}", id);

    return dataList.removeIf(product -> product.getId().equals(id));
  }

  @Tool(description = "获取指定订单号的订单详情")
  public Order getAirQuality(@ToolParam(description = "订单号") String orderId) {
    return restTemplate.getForObject("http://order-service/order?id" + orderId, Order.class);
  }

  /** 模拟数据库查询 */
  private List<Product> queryListByCondition(Product req) {
    return dataList.stream()
        .filter(
            product ->
                product.getId().equals(req.getId()) || product.getName().contains(req.getName()))
        .toList();
  }

  /** 模拟数据库数据 */
  public static List<Product> mockDBData() {
    List<Product> productList = new ArrayList<>();
    // 静态测试数据
    productList.add(
        new Product(1L, "Apple iPhone 15 Pro", new BigDecimal("1299.99"), "最新苹果旗舰手机，支持5G", 30));
    productList.add(
        new Product(
            2L, "Samsung Galaxy S24 Ultra", new BigDecimal("1399.99"), "最新三星旗舰手机，支持5G", 25));
    productList.add(
        new Product(3L, "Dell XPS 15", new BigDecimal("1799.99"), "高性能轻薄笔记本电脑，适合办公和娱乐", 40));
    productList.add(
        new Product(4L, "Sony WH-1000XM5", new BigDecimal("399.99"), "降噪耳机，适合旅行和日常使用", 50));
    productList.add(
        new Product(5L, "Nintendo Switch OLED", new BigDecimal("299.99"), "便携式游戏机，支持OLED屏幕", 60));
    productList.add(
        new Product(6L, "Apple Watch Series 9", new BigDecimal("349.99"), "智能手表，支持健康监测和运动追踪", 70));
    productList.add(
        new Product(7L, "Logitech MX Master 3", new BigDecimal("99.99"), "无线蓝牙鼠标，适合办公和游戏", 80));
    productList.add(
        new Product(8L, "Bose QuietComfort 45", new BigDecimal("299.99"), "降噪耳机，适合长时间佩戴", 35));
    productList.add(
        new Product(
            9L, "Microsoft Surface Pro 9", new BigDecimal("1199.99"), "二合一平板电脑，适合办公和创意设计", 20));
    productList.add(
        new Product(10L, "Canon EOS R6", new BigDecimal("2499.99"), "全画幅无反相机，适合专业摄影", 15));
    return productList;
  }
}
